import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

dfuserpoint=pd.read_excel('发票识别导出-筛选.xlsx')
#dfuserpoint=pd.read_csv('发票识别导出-筛选.csv')

outpd = pd.DataFrame(dfuserpoint,columns=['大单号','主店片区','主店号','主店名','主店所在公司','现金','公司礼券','商场储值','订金销账','积分抵现','商场礼券','活动名称','日期','银行卡','小票网址'])
outpd.insert(loc=15,column='result',value='')
outpd.insert(loc=16,column='url',value='')


import requests

url_heard1 = "http://localhost:10111"
url_heard = "http://192.168.12.46:8888"
url_heard2 = "http://192.168.12.107:10111"

def check_receipt_post(url,date,amount):
    check_url = url_heard2+"/ReceiptORC/check_receipt"
    req = requests.post(check_url,data={"date":date,"amount":amount,"fileUrl":url})  # 发post请求,以json字符串参数格式
    return req.text


for index, row in outpd.iterrows():
    # datestr =row['日期'].replace("/", "-")
    datestr = row['日期'].strftime("%Y-%m-%d")
    amount = row['银行卡']
    if row['银行卡'] == 0:
        if row['现金'] != 0:
            amount = row['现金']
    if not isinstance(row['小票网址'], str):
        outpd.iloc[index, outpd.columns.get_loc("result")] = ''
        outpd.iloc[index, outpd.columns.get_loc("url")] = ''
        continue
    urlstr = row['小票网址'];
    amountstr = ('%f' % amount).rstrip('0').rstrip('.')
    result = check_receipt_post(row['小票网址'], datestr, amountstr)
    #    row['result'] = result.split(',')[0]
    #    row['url'] = result.split(',')[1]

    if result.find("<")>=0:
        result = "图片验证失败,报错"
    print(result)
    resource_file_name = row['小票网址'].split("/")[len(row['小票网址'].split("/")) - 1]
    result_url = url_heard + result.split(',')[1]
    outpd.iloc[index, outpd.columns.get_loc("result")] = result.split(',')[0]
    outpd.iloc[index, outpd.columns.get_loc("url")] = result_url
    print(index, row['大单号'],datestr,amountstr, result.split(',')[0],result_url)

with pd.ExcelWriter('out-发票识别导出1.xlsx') as writer:
    outpd.to_excel(writer, sheet_name='Sheet1')
